package br.com.ia.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.enovar.trex.struts.action.TRexBaseAction;
import br.com.ia.business.ValorBusiness;
import br.com.ia.business.VariavelBusiness;
import br.com.ia.model.Valor;
import br.com.ia.model.Variavel;
import br.com.ia.struts.form.ValorForm;

public class ValorAction extends TRexBaseAction {

	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ValorForm valorForm = (ValorForm) form;
		valorForm.setState("search");
		valorForm.setMethod("search");

		VariavelBusiness variavelBusiness = (VariavelBusiness) getBusinessClass(VariavelBusiness.class);
		Variavel variavel = (Variavel) variavelBusiness.retrieveById(valorForm
				.getCodigoVariavelPai());

		List<Valor> list = variavel.getValores();

		request.setAttribute("list", list);

		return mapping.getInputForward();
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ValorForm valorForm = (ValorForm) form;
		valorForm.setState("search");
		valorForm.setMethod("search");

		ValorBusiness valorBusiness = (ValorBusiness) getBusinessClass(ValorBusiness.class);
		
		Valor valor = new Valor();
		valor.setCodigo(valorForm.getCodigo());
		
		valorBusiness.delete(valor);

		return search(mapping, valorForm, request, response);
	}

}
